package cn.byapps.chensheng.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.byapps.chensheng.dao.WxAccountDao;
import cn.byapps.chensheng.dao.WxMaterialDao;
import cn.byapps.chensheng.dao.WxMaterialNewsArticleDao;
import cn.byapps.chensheng.dao.WxMaterialNewsDao;
import cn.byapps.chensheng.entity.WxAccount;
import cn.byapps.chensheng.entity.WxMaterial;
import cn.byapps.chensheng.entity.WxMaterialNewsArticle;
import cn.byapps.chensheng.service.IndexService;
import cn.byapps.chensheng.vo.WxAccoutVO;
import cn.byapps.chensheng.vo.WxNewVO;

@Service
@Transactional
public class IndexServiceImpl implements IndexService {

    @Autowired
    private WxAccountDao wxAccountDao;
    @Autowired
    private WxMaterialDao wxMaterialDao;
    @Autowired
    private WxMaterialNewsDao wxMaterialNewsDao;
    @Autowired
    private WxMaterialNewsArticleDao wxMaterialNewsArticleDao;

    @Override
    public List<WxAccoutVO> findAll() {
        List<WxAccoutVO> accoutvos = new ArrayList<>();
        List<WxAccount> accounts = wxAccountDao.findAll();
        for (WxAccount wxAccount : accounts) {
            WxAccoutVO wxAccountVO = new WxAccoutVO();
            BeanUtils.copyProperties(wxAccount, wxAccountVO);
            List<WxMaterial> materials = wxMaterialDao.findbyAppId(wxAccount.getAppId());
            List<WxNewVO> news = new ArrayList<WxNewVO>();
            for (WxMaterial wxMeterial : materials) {
                WxMaterialNewsArticle wxMaterialNewsArticle = wxMaterialNewsArticleDao.getById(wxMeterial.getMediaId());
                WxNewVO wxNewVO = new WxNewVO();
                BeanUtils.copyProperties(wxMaterialNewsArticle, wxNewVO);
                news.add(wxNewVO);
            }
            wxAccountVO.setItems(news);
            accoutvos.add(wxAccountVO);
        }
        return accoutvos;
    }

    @Override
    public List<WxAccoutVO> findByPage(Integer page, Integer rows) {
        List<WxAccoutVO> accoutvos = new ArrayList<>();
        List<WxAccount> accounts = wxAccountDao.findAll();
        for (WxAccount wxAccount : accounts) {
            WxAccoutVO wxAccountVO = new WxAccoutVO();
            BeanUtils.copyProperties(wxAccount, wxAccountVO);
            List<WxMaterial> materials = wxMaterialDao.findPageByAppid(wxAccount.getAppId(), page, rows);
            List<WxNewVO> news = new ArrayList<WxNewVO>();
            for (WxMaterial wxMeterial : materials) {
                WxMaterialNewsArticle wxMaterialNewsArticle = wxMaterialNewsArticleDao.getById(wxMeterial.getMediaId());
                WxNewVO wxNewVO = new WxNewVO();
                BeanUtils.copyProperties(wxMaterialNewsArticle, wxNewVO);
                wxNewVO.setUpdateTime(wxMeterial.getUpdateTime());
                news.add(wxNewVO);
            }
            if (news.size() > 0) {
                wxAccountVO.setItems(news);
                accoutvos.add(wxAccountVO);
            }

        }
        return accoutvos;
    }

    @Override
    public int countAppId(String appid) {
        Integer total = wxMaterialDao.countByAppId(appid);
        return total;
    }

    @Override
    public List<WxNewVO> findPageByAppId(String appid, Integer page, Integer rows) {
        List<WxMaterial> materials = wxMaterialDao.findPageByAppid(appid, page, rows);
        List<WxNewVO> news = new ArrayList<WxNewVO>();
        for (WxMaterial wxMeterial : materials) {
            WxMaterialNewsArticle wxMaterialNewsArticle = wxMaterialNewsArticleDao.getById(wxMeterial.getMediaId());
            WxNewVO wxNewVO = new WxNewVO();
            BeanUtils.copyProperties(wxMaterialNewsArticle, wxNewVO);
            wxNewVO.setUpdateTime(wxMeterial.getUpdateTime());
            news.add(wxNewVO);
        }
        return news;
    }

    @Override
    public WxAccoutVO getAccountByAppId(String appid) {
        List<WxAccount> accounts = wxAccountDao.findByAppId(appid);
        if (accounts != null && accounts.size() > 0) {
            WxAccoutVO wxAccoutVO = new WxAccoutVO();
            BeanUtils.copyProperties(accounts.get(0), wxAccoutVO);
            return wxAccoutVO;
        }
        return null;
    }

}